﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Social.Core.Contracts;

namespace Social.Core
{
    /// <summary>
    /// provides methods to format a request
    /// </summary>
    public class RequestFormatter : IRequestFormatter
    {
        /// <summary>
        /// formats an url 
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="objectId">objectId</param>
        /// <param name="args">arguments to add to the querystring</param>
        /// <returns>formatted url</returns>
        public string FormatUrl(string url, string objectId, IDictionary<String, String> args)
        {

            string urlResult = String.Empty;
            StringBuilder queryString = new StringBuilder();

            if (args != null)
            {
                foreach (string key in args.Keys)
                    queryString.Append(key + "=" + args[key] + "&");

                if (queryString.Length > 0)
                    queryString.Remove(queryString.Length - 1, 1);
            }

            if (objectId != null)
                urlResult = string.Format(url, objectId);
            else
                urlResult = url;

            return urlResult + (queryString != null ? "?" + queryString.ToString() : String.Empty);
        }

    }
}
